package com.gofaraway.start.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ChenPeng
 * @Date 2024/9/4 21:29
 */
@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private int database;

    @Value("${spring.redis.port}")
    private int port;

    /**
     *  连接redis host前缀
     */
    private static final String REDIS_HOST_PERFIX = "redis://";
    /**
     * 端口分隔符
     */
    private static final String PORT_SPLIT = ":";
    /**
     * 最大连接大小，默认64
     */
    private static final int CONNECTION_POOL_SIZE = 200;
    /**
     * 最小空闲连接大小，默认32
     */
    private static final int CONNECTION_MINIMUM_IDLE_SIZE = 10;

    @Bean
    public RedissonClient redissonClient(){
        Config config = new Config();

        config.useSingleServer()
                .setAddress(REDIS_HOST_PERFIX + host + PORT_SPLIT + port)
                .setPassword(password)
                .setDatabase(database)
                .setConnectionPoolSize(CONNECTION_POOL_SIZE)
                .setConnectionMinimumIdleSize(CONNECTION_MINIMUM_IDLE_SIZE);

        return Redisson.create(config);

    }
}
